package org.acme.service;

import io.smallrye.mutiny.Uni;
import io.vertx.core.json.JsonObject;
import org.acme.client.RestClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.io.InputStream;
import java.time.Duration;
import java.util.concurrent.Executor;
import java.util.concurrent.ForkJoinPool;

@ApplicationScoped
public class ReactiveGreetingService {


    private static final Logger LOGGER = LoggerFactory.getLogger(ReactiveGreetingService.class.getName());


    @Inject
    RestClient restClient;

    private static Executor executor = new ForkJoinPool();


    public Uni<String> getByName(String name) {
        LOGGER.info("获取数据 "+name);
        return Uni.createFrom().item(restClient.getStream(name))
                .onItem().transformToUni(this::invokeRemoteGreetingService)
                ; // 1
    }

    Uni<String> invokeRemoteGreetingService(String inputStream) {
        LOGGER.info("====nputstream:"+inputStream);
        return Uni.createFrom().item(inputStream)
                .emitOn(executor)
                .onItem().delayIt().by(Duration.ofMillis(1))
                .onItem().transform(s -> {
                    return s;
                });
    }

}

